<template>
    <div class="color-picker-box" id="color-picker-panel" @click.stop="preventPropagation">
        <div class="color-list">
            <div v-for="(item, index) in colorList" :key="index" class="colorItem" :style="{background: item}" @click="getItemColor(item)">
                <a-icon type="check" v-if="item.toLowerCase() === currentColor.toLowerCase()" class="colorItem-active"/>
            </div>
            <div v-if="!isdefaultColor" class="custom-active" @click="onCustomSet" :style="{background: currentColor,border:`solid 1px ${currentColor}`}">
                <a-icon type="check" class="custom-colorItem-active"/>
                <span>自定义</span>
            </div>
            <div v-else class="custom" @click="onCustomSet">自定义</div>
        </div>
        <div class="color-panel-box" v-if="visible">
            <div class="color-panel-wrap" >
                <div class="color-panel" :style="{background: colorPanelHEX}" id="color-panel">
                    <div class="saturation-white">
                        <div class="saturation-black">
                        </div>
                        <div class="pickerBtn" id="color-panel-picker">
                            <div class="pickerBtn-content">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="color-bar-wrap">
                <div class="flexbox-fix">
                    <div class="color-bar-content">
                        <div class="color-bar-content-wrap">
                            <div class="color-bar-box" id="color-bar">
                                <div class="color-bar-horizontal">
                                    <div class="colorBar-color-picker" id="color-bar-picker">
                                        <div class="colorBar-color-picker-btn" :style="{background: currentColor}"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="selected-color-content">
                        <div class="colorpicker-showColor" :style="{background: currentColor}"></div>
                    </div>
                </div>
            </div>
            <div class="color-edit-wrap">
                <div class="color-select-wrap">
                    <a-select class="color-select" v-model="current_mode" :getPopupContainer="triggerNode => triggerNode.parentNode">
                        <a-select-option style="font-size:12px" :value="1">HEX</a-select-option>
                        <a-select-option style="font-size:12px" :value="2">RGB</a-select-option>
                        <a-select-option style="font-size:12px" :value="3">HSB</a-select-option>
                    </a-select>
                </div>
                <div class="color-input-wrap">
                    <a-input v-model="hex" v-if="current_mode === 1" class="color-input" @change="onHexChange" :maxLength="7" required="required" ></a-input>
                    <a-input-group  v-else-if="current_mode === 2" compact class="color-input" @change="onRgbChange">
                        <a-input v-model.number="rgb.r" style="width: 33%;height:100%;font-size: 12px;" :maxLength="3" required="required"/>
                        <a-input v-model.number="rgb.g" style="width: 33%;height:100%;font-size: 12px;" :maxLength="3" required="required"/>
                        <a-input v-model.number="rgb.b" style="width: 33%;height:100%;font-size: 12px;" :maxLength="3" required="required"/>
                    </a-input-group>
                    <a-input-group v-else compact class="color-input" @change="onRsbChange">
                        <a-input v-model.number="hsb.h" style="width: 33%;height:100%;font-size: 12px;" :maxLength="3" required="required"/>
                        <a-input v-model.number="hsb.s" style="width: 33%;height:100%;font-size: 12px;" :maxLength="3" required="required"/>
                        <a-input v-model.number="hsb.b" style="width: 33%;height:100%;font-size: 12px;" :maxLength="3" required="required"/>
                    </a-input-group>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: "vColorPicker",
    data() {
        return {
            colorList: ["#8ec5fe", "#8998fd", "#75dbd9", "#fecd8e", "#febb8e", "#fe8e8e", "#ab8efe", "#f3e09f", "#fe8ed5", "#8ddef3", "#c28efe", "#c5cdd6"],
            visible: false,
            hex: {},
            rgb: { r: 0, g: 0, b: 0},
            hsb: { h: 0, s: 100, b: 100 },
            // 选中的颜色
            currentColor: "",
            // 取色板的颜色
            colorPanelHEX: "",
            // 1 HEX 2 RGB 3 HSB
            current_mode: 1,
            // 取色板
            colorPanel: null,
            colorPanelPicker: null,
            // 取色条
            colorBar: null,
            colorBarPicker: null,
            colorPanelWidth: "",
            colorPanelHeight: "",
            colorBarWidth: "",
            // 起始位置
            x: 0,
            y: 0
        }
    },
    props: {
        defaultColor: {
            type: String,
            default: "#8ec5fe"
        }
    },
    computed: {
        isdefaultColor() {
            return this.colorList.includes(this.currentColor.toUpperCase()) || this.colorList.includes(this.currentColor.toLowerCase())
        }
    },
    watch: {
        defaultColor: {
            handler(nVal) {
                this.currentColor = nVal
            },
            immediate: true
        },
        currentColor: {
            handler(nVal) {
                this.$emit('getColor', nVal)
            },
            immediate: true
        },
        current_mode(nVal, oVal) {
            if(oVal === 1 && nVal === 2) {
                this.rgb = this.HEXToRGB(this.hex)
            }
            if(oVal === 2 && nVal === 1) {
                this.hex = this.RGBToHEX(this.rgb)
            }
            if(oVal === 2 && nVal === 3) {
                this.hsb = this.RGBToHSB(this.rgb)
            }
            if(oVal === 3 && nVal === 2) {
                this.rgb = this.HSBToRGB(this.hsb)
            }
            if(oVal === 3 && nVal === 1) {
                this.hex = this.HSBToHEX(this.hsb)
            }
            if(oVal === 1 && nVal === 3) {
                this.hsb = this.HEXToHSB(this.hex)
            }
        },
        visible(nVal) {
            if(nVal) {
                document.body.addEventListener('click', () => {
                    this.visible = false
                })
            }
        }
    },
    methods: {
        preventPropagation() { return false },
        onHexChange() {
            this.currentColor = this.hex;
            this.hsb = this.HEXToHSB(this.hex);
            if(this.hsb.s < 5 && this.hsb.b > 94) {
                this.hsb.s = 0;
                this.hsb.b = 94;
                this.currentColor = this.hex = this.HSBToHEX(this.hsb);
            }
            this.rgb = this.HEXToRGB(this.hex);
            this.setPosition();
        },
        onRgbChange() {
            this.rgb.r = this.rgb.r > 255 ? 255 : this.rgb.r;
            this.rgb.g = this.rgb.g > 255 ? 255 : this.rgb.g;
            this.rgb.b = this.rgb.b > 255 ? 255 : this.rgb.b;
            if(this.rgb.r > 240 && this.rgb.g > 240 && this.rgb.b > 240) {
                this.rgb.r = 240;
                this.rgb.g = 240;
                this.rgb.b = 240;
            }
            this.currentColor = this.RGBToHEX(this.rgb);
            this.hex = this.RGBToHEX(this.rgb);
            this.hsb = this.RGBToHSB(this.rgb);
            this.setPosition();
        },
        onRsbChange() {
            this.hsb.h = this.hsb.h > 360 ? 360 : this.hsb.h;
            this.hsb.s = this.hsb.s > 100 ? 100 : this.hsb.s;
            this.hsb.b = this.hsb.b > 100 ? 100 : this.hsb.b;
            if(this.hsb.b > 94 && this.hsb.s < 5) {
                this.hsb.s = 0;
                this.hsb.b = 94;
            }
            this.currentColor = this.HSBToHEX(this.hsb);
            this.rgb = this.HSBToRGB(this.hsb);
            this.hex = this.HSBToHEX(this.hsb);
            this.setPosition();
        },
        initData() {
            this.hex = this.currentColor;
            this.rgb = this.HEXToRGB(this.currentColor);
            this.hsb = this.HEXToHSB(this.currentColor);
            this.$nextTick(() => {
                // 获取DOM元素
                this.colorPanel  = document.querySelector('#color-panel');
                this.colorPanelPicker  = document.querySelector('#color-panel-picker');
                this.colorBar  = document.querySelector('#color-bar');
                this.colorBarPicker = document.querySelector('#color-bar-picker');
                // 容器的宽高
                this.colorPanelWidth = this.colorPanel.offsetWidth;
                this.colorPanelHeight = this.colorPanel.offsetHeight;
                this.colorBarWidth = this.colorBar.offsetWidth;
                // 初始化取色板的颜色
                // 初始化取色板和取色条按钮的位置
                this.setPosition();
                this.bindMove(this.colorBar, false, this.colorBarPicker);
                this.bindMove(this.colorPanel, true, this.colorPanelPicker);
            })
        },
        getItemColor(colorStr) {
            this.currentColor = colorStr;
            this.hex = colorStr;
            this.rgb = this.HEXToRGB(colorStr);
            this.hsb = this.HEXToHSB(colorStr);
            this.setPosition();
        },
        setPosition() {
            this.colorPanelHEX = this.HSBToHEX({h: this.hsb.h, s: 100, b: 100})
            if(this.colorBarPicker) {
                this.colorBarPicker.style.left = this.hsb.h * this.colorBarWidth / 360 + "px";
                this.colorPanelPicker.style.left = this.hsb.s * this.colorPanelWidth / 100 + "px";
                this.colorPanelPicker.style.top = this.colorPanelHeight - this.hsb.b * this.colorPanelHeight / 100  + "px";
            }
        },

        bindMove(elem, bool, elemChild) {
            elem.addEventListener("mousedown", (e) => {
                // 禁止用户选择网页中文字
                document.onselectstart = () => false;
                // 禁止用户拖动元素
                document.ondragstart = () => false;
                let startX = elem.getBoundingClientRect().left;
                let startY = elem.getBoundingClientRect().top;
                bool ? this.setPanel(elemChild, e, startX, startY) : this.setBar(elemChild, e, startX);
                document.addEventListener("mousemove", mousemove, false);
                const that = this;
                function mousemove(e) {
                    bool ? that.setPanel(elemChild, e, startX, startY) : that.setBar(elemChild, e, startX);
                    e.preventDefault();
                }
                document.addEventListener("mouseup", mouseup, false);
                function mouseup() {
                    document.removeEventListener("mousemove", mousemove, false)
                    document.removeEventListener("mouseup", mouseup, false)
                    // 允许用户选择网页中文字
                    document.onselectstart = null
                    // 允许用户拖动元素
                    document.ondragstart = null
                }
            }, false);
        },
        setBar(elemChild, e, x) {
            let left = e.pageX - x;
            if(left < 0) left = 0
            if (left > this.colorBarWidth) left = this.colorBarWidth
            elemChild.style.left = left + "px"
            // 选中的颜色
            this.hsb.h = Math.round(360 * left / this.colorBarWidth);
            this.currentColor = this.HSBToHEX(this.hsb);
            this.hex = this.HSBToHEX(this.hsb);
            this.rgb = this.HSBToRGB(this.hsb);
            // 色板颜色
            let hsbTemp = {h: 0, s: 100, b: 100}
            hsbTemp.h = Math.round(360 * left / this.colorBarWidth);
            this.colorPanelHEX = this.HSBToHEX(hsbTemp);
        },
        setPanel(elemChild, e, x, y) {
            let left = e.pageX - x;
            let top = e.pageY - y;
            if(left < 0) left = 0
            if (left > this.colorPanelWidth) left = this.colorPanelWidth
            if(top < 0) top = 0
            if (top > this.colorPanelHeight) top = this.colorPanelHeight
            elemChild.style.left = left + "px"
            elemChild.style.top = top + "px"
            this.hsb.s = Math.round(100 * left / this.colorPanelWidth);
            this.hsb.b = Math.round(100 * (this.colorPanelHeight - top) / this.colorPanelHeight);

            if(this.hsb.b > 94 && this.hsb.s < 5) {
                this.hsb.b = 94;
                this.hsb.s = 0;
            }

            this.currentColor = this.HSBToHEX(this.hsb);
            this.hex = this.HSBToHEX(this.hsb);
            this.rgb = this.HSBToRGB(this.hsb);
        },
        onCustomSet() {
            this.visible = true;
            if(this.visible) {
                this.initData();
            }
        },
        HSBToRGB(hsb) {
            var rgb = {r: 0, g: 0, b: 0};
            var h = Math.round(hsb.h);
            var s = Math.round(hsb.s * 255 / 100);
            var v = Math.round(hsb.b * 255 / 100);
            if (s === 0) {
                rgb.r = rgb.g = rgb.b = v;
            } else {
                var t1 = v;
                var t2 = (255 - s) * v / 255;
                var t3 = (t1 - t2) * (h % 60) / 60;
                if (h === 360) h = 0;
                if (h < 60) { rgb.r = t1; rgb.b = t2; rgb.g = t2 + t3 }
                else if (h < 120) { rgb.g = t1; rgb.b = t2; rgb.r = t1 - t3 }
                else if (h < 180) { rgb.g = t1; rgb.r = t2; rgb.b = t2 + t3 }
                else if (h < 240) { rgb.b = t1; rgb.r = t2; rgb.g = t1 - t3 }
                else if (h < 300) { rgb.b = t1; rgb.g = t2; rgb.r = t2 + t3 }
                else if (h < 360) { rgb.r = t1; rgb.g = t2; rgb.b = t1 - t3 }
                else { rgb.r = 0; rgb.g = 0; rgb.b = 0 }
            }
            return { r: Math.round(rgb.r), g: Math.round(rgb.g), b: Math.round(rgb.b) };
        },
        RGBToHSB(rgb) {
            var hsb = { h: 0, s: 0, b: 0 };
            var min = Math.min(rgb.r, rgb.g, rgb.b);
            var max = Math.max(rgb.r, rgb.g, rgb.b);
            var delta = max - min;
            hsb.b = max;
            hsb.s = max !== 0 ? 255 * delta / max : 0;
            if (hsb.s !== 0) {
                if (rgb.r === max) hsb.h = (rgb.g - rgb.b) / delta;
                else if (rgb.g === max) hsb.h = 2 + (rgb.b - rgb.r) / delta;
                else hsb.h = 4 + (rgb.r - rgb.g) / delta;
            } else hsb.h = -1;
            hsb.h = Math.round(hsb.h * 60);
            if (hsb.h < 0) hsb.h += 360;
            hsb.s = Math.round(hsb.s * 100 / 255);
            hsb.b = Math.round(hsb.b * 100 / 255);
            return hsb;
        },
        RGBToHEX(rgb) {
            var hex = [
                rgb.r.toString(16),
                rgb.g.toString(16),
                rgb.b.toString(16)
            ];
            hex.map(function (str, i) {
                if (str.length === 1) {
                    hex[i] = '0' + str;
                }
            });
            return "#" + hex.join('');
        },
        HEXToRGB(hex) {
            let hexTemp = parseInt(hex.indexOf('#') > -1 ? hex.substring(1) : hex, 16);
            return { r: hexTemp >> 16, g: (hexTemp & 0x00FF00) >> 8, b: hexTemp & 0x0000FF };
        },
        HEXToHSB(hex) {
            return this.RGBToHSB(this.HEXToRGB(hex));
        },
        HSBToHEX(hsb) {
            return this.RGBToHEX(this.HSBToRGB(hsb));
        }
    }
}

</script>
<style lang="less" scoped>
.color-picker-box{
    position: relative;
    height: 70px;
    width: 223px;
    border: 1px solid rgb(214, 220, 226);
    border-radius: 5px;
    .color-list{
        display: flex;
        flex-wrap: wrap;
        padding: 8px;
        height: 100%;
        .colorItem{
            position: relative;
            height: 24px;
            width: 24px;
            border-radius: 4px;
            margin-right: 6px;
            margin-bottom: 5px;
            transition: all 0.1s;
            cursor: pointer;
            &:hover {
                transform: scale(1.1);
            }
        }
        .colorItem:nth-of-type(7){
            margin-right: 0px;
        }
        .colorItem-active{
            color: rgb(255, 255, 255);
            font-size: 12px;
            text-align: center;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
        }
        .custom{
            line-height: 22px;
            text-align: center;
            color: #445160;
            width: 54px;
            height: 24px;
            font-size: 12px;
            background-color: #fff;
            border-radius: 4px;
            border: solid 1px #c5cdd6;
            cursor: pointer;
            user-select: none;
            &:hover{
                color: #1c79f4;
                border: solid 1px #1c79f4;
            }
        }
        .custom-active{
            position: relative;
            display: flex;
            justify-content: space-around;
            align-items: center;
            text-align: center;
            line-height: 22px;
            text-align: center;
            color: rgb(111, 170, 247);
            width: 54px;
            height: 24px;
            font-size: 12px;
            color: rgb(255, 255, 255);
            background-color: #fff;
            border-radius: 4px;
            border: solid 1px #1c79f4;
            user-select: none;
            cursor: pointer;
        }
        .custom-colorItem-active{
            font-size: 12px;
            text-align: center;
        }
    }
    .color-panel-box{
        padding: 12px;
        position: absolute;
        top: 70px;
        width: 222px;
        height: 256px;
        background: #fff;
        z-index: 9;
        border-radius: 5px;;
        box-shadow: 0 10px 32px 0 rgba(0, 0, 0, 0.16);
        .saturation-white {
            position: absolute;
            top: 0px;
            right: 0px;
            bottom: 0px;
            left: 0px;
            background: linear-gradient(to right, #fff, rgba(255,255,255,0));
        }
        .saturation-black {
            position: absolute;
            top: 0px;
            right: 0px;
            bottom: 0px;
            left: 0px;
            background: linear-gradient(to top, #000, rgba(0,0,0,0));
        }
        .color-panel-wrap{
            width: 100%;
            height: 160px;
            position: relative;
            border-radius: 2px 2px 0px 0px;
            overflow: hidden;
            .color-panel{
                position: absolute;
                top: 0px;
                right: 0px;
                bottom: 0px;
                left: 0px;
                .pickerBtn{
                    position: absolute;
                    top: 0%;
                    left: 100%;
                    cursor: default;
                    .pickerBtn-content{
                        width: 12px;
                        height: 12px;
                        border-radius: 6px;
                        box-shadow: rgb(255, 255, 255) 0px 0px 0px 1px inset;
                        transform: translate(-6px, -6px);
                    }
                }
            }
        }
        .color-bar-wrap{
            padding: 10px 0 9px 0;
            .flexbox-fix{
                display: flex;
                align-items: center;
                height: 28px;
                .selected-color-content{
                    position: relative;
                    overflow: hidden;
                    width: 28px;
                    height: 28px;
                    border-radius: 2px;
                    .colorpicker-showColor{
                        position: absolute;
                        top: 0px;
                        right: 0px;
                        bottom: 0px;
                        left: 0px;
                    }
                }
                .color-bar-content{
                    -webkit-box-flex: 1;
                    flex: 1 1 0%;
                    .color-bar-content-wrap{
                        position: relative;
                        height: 8px;
                        width: 158px;
                        .color-bar-box{
                            position: absolute;
                            top: 0px;
                            right: 0px;
                            bottom: 0px;
                            left: 0px;
                            .color-bar-horizontal {
                                padding: 0px 2px;
                                position: relative;
                                height: 100%;
                                border-radius: 10px;
                                background: linear-gradient(to right,
                                    #ff0c09 0%,
                                    #ffac01 11%,
                                    #ffff00 17%,
                                    #7cff00 25%,
                                    #00ff2d 36%,
                                    #00ffa8 44%,
                                    #00e2ff 52%,
                                    #0027fb 64%,
                                    #8300ff 75%,
                                    #ff00ff 83%,
                                    #ff0092 90%,
                                    #ff090c 100%);
                                .colorBar-color-picker{
                                    position: absolute;
                                    left: 0%;
                                    .colorBar-color-picker-btn{
                                        width: 16px;
                                        height: 16px;
                                        border-radius: 8px;
                                        transform: translate(-6px, -4px);
                                        // background-color: #0cfefa;
                                        box-shadow: 0px 2px 4px 0px
                                            rgba(0, 0, 0, 0.2);
                                        border: solid 2px #ffffff;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        .color-edit-wrap{
            display: flex;
            height: 24px;
            .color-select-wrap{
                margin-right: 10px;
                .color-select{
                    width: 60px;
                    height: 24px;
                    font-size: 12px;
                    /deep/.ant-select-selection{
                        height: 100%;
                        .ant-select-selection-selected-value{
                            padding-right: 0;
                            height: 24px;
                            line-height: 23px;
                        }
                    }
                }
            }
            .color-input-wrap{
                .color-input{
                    font-size: 12px;
                    width: 128px;
                    height: 24px;
                    text-align: center;
                }
            }
        }
    }
}
</style>
